home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / sml_nj / 93src.lha / src / runtime / timers.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-09  |  3.7 KB  |  175 lines

  1. /* timers.c
  2.  *
  3.  * COPYRIGHT (c) 1990 by AT&T Bell Laboratories.
  4.  */
  5.  
  6. #include "ml_os.h"
  7.  
  8. #if defined (V9) || defined(HPUX)
  9. #include <sys/times.h>
  10. #else
  11. #ifndef THINK_C
  12. #include <sys/time.h>
  13. #else
  14. #include <time.h>
  15. #include "MacOS.dep.h"
  16. #endif
  17. #ifdef AUX
  18. #include <sys/times.h>
  19. #else
  20. #ifndef THINK_C
  21. #include <sys/resource.h>
  22. #endif
  23. #endif
  24. #endif
  25.  
  26. #include "ml_types.h"
  27.  
  28. #ifdef THINK_C
  29. /* 28Jan93   Kjeld & Soren */
  30.  
  31. #define TMacbaseyr      1904L
  32. #define TUNIXbaseyr     1970L
  33.  
  34. /* number of leap days between the two years -- Mac base was a leap year! */
  35.  
  36. #define TLpD    ((TUNIXbaseyr-TMacbaseyr-1)/4) + 1
  37.  
  38. /* TimeBaseDif is the number of seconds between Mac and UNIX time (GMT) */
  39.  
  40. #define TimeBaseDif     ((((TUNIXbaseyr-TMacbaseyr)*365)+TLpD)*24*60*60)
  41.  
  42.  
  43. void gettimeofday ( struct timeval *t, int huh )
  44. {
  45.         unsigned long now, ticks;
  46.         
  47.         GetDateTime(&now);
  48.         ticks = clock();
  49.         
  50.         t->tv_sec = now - TimeBaseDif;
  51.         
  52.     t->tv_usec = ((ticks % CLOCKS_PER_SEC) * 1000000) / CLOCKS_PER_SEC;
  53. }
  54. #endif
  55.  
  56. /* The global clocks (as ML ints) */
  57. ML_val_t    t_sec, t_usec,    /* total usr time */
  58.         s_sec, s_usec,    /* total sys time */
  59.         g_sec, g_usec;    /* garbage collection usr time */
  60.  
  61. static struct timeval gt0,    /* the startime of the most recent garbage collection */
  62.         garbagetime;    /* the cumulative garbage collection time */
  63.  
  64. /* get_cpu_time:
  65.  * Get the user and/or system cpu times in a system independent way.
  66.  */
  67. static void get_cpu_time (user_t, sys_t)
  68.     struct timeval  *user_t, *sys_t;
  69. {
  70. #if defined(V9) || defined(HPUX) || defined(AUX)
  71.     struct tms        ts;
  72.     times (&ts);
  73.     if (user_t) {
  74.     user_t->tv_usec    = ((ts.tms_utime % 60) * 1000000) / 60;
  75.     user_t->tv_sec    = (ts.tms_utime / 60);
  76.     }
  77.     if (sys_t) {
  78.     sys_t->tv_usec    = ((ts.tms_stime % 60) * 1000000) / 60;
  79.     sys_t->tv_sec    = (ts.tms_stime / 60);
  80.     }
  81. #else
  82. #ifdef THINK_C
  83.     int    ticks;
  84.     if (user_t) {
  85.         ticks = (int)clock();
  86.         user_t->tv_sec  = ticks / CLOCKS_PER_SEC;
  87.         user_t->tv_usec = ((ticks % CLOCKS_PER_SEC) * 1000000) / CLOCKS_PER_SEC;
  88.     }
  89.     if (sys_t)  {
  90.     sys_t->tv_usec    = 0;
  91.     sys_t->tv_sec    = 0;
  92.     }
  93. #else
  94.     struct rusage   r;
  95.     getrusage (RUSAGE_SELF, &r);
  96.     if (user_t) { *user_t = r.ru_utime; }
  97.     if (sys_t) { *sys_t = r.ru_stime; }
  98. #endif
  99. #endif
  100. } /* end of get_cpu_time. */
  101.  
  102.  
  103. /* start_gc_timer:
  104.  * Start timing a garbage collection.
  105.  */
  106. void start_gc_timer ()
  107. {
  108.     get_cpu_time (>0, 0);
  109. }
  110.  
  111. /* check_gc_timer:
  112.  * Return the time (in ms.) spent since the start of the most recent
  113.  * garbage collection.
  114.  */
  115. int check_gc_timer ()
  116. {
  117.     struct timeval t1;
  118.     int        sec, usec;
  119.  
  120.     get_cpu_time (&t1, 0);
  121.     sec = t1.tv_sec - gt0.tv_sec;
  122.     usec = t1.tv_usec - gt0.tv_usec;
  123.     if (usec < 0) {
  124.     sec--; usec += 1000000;
  125.     }
  126.     return (usec/1000 + sec*1000);
  127. }
  128.  
  129. /* stop_gc_timer:
  130.  * Stop the garbage collection timer and update the cumulative garbage collection
  131.  * time.
  132.  */
  133. void stop_gc_timer ()
  134. {
  135.     int        sec, usec;
  136.     struct timeval t1;
  137.  
  138.     get_cpu_time (&t1, 0);
  139.     sec = (t1.tv_sec - gt0.tv_sec) + garbagetime.tv_sec;
  140.     usec = (t1.tv_usec - gt0.tv_usec) + garbagetime.tv_usec;
  141.     if (usec < 0) {
  142.     sec--; usec += 1000000;
  143.     }
  144.     else if (usec > 1000000) {
  145.     sec++; usec -= 1000000;
  146.     }
  147.     garbagetime.tv_sec = sec;
  148.     garbagetime.tv_usec = usec;
  149. }
  150.  
  151. /* resettimers:
  152.  * Clear the timers.
  153.  */
  154. resettimers()
  155. {
  156.     garbagetime.tv_sec = 0;
  157.     garbagetime.tv_usec = 0;
  158. }
  159.  
  160. /* timer:
  161.  * Set the ML version of the global clock.
  162.  */
  163. timer()
  164. {
  165.     struct timeval    t, s;
  166.  
  167.     get_cpu_time (&t, &s);
  168.     t_usec    = INT_CtoML(t.tv_usec);
  169.     t_sec    = INT_CtoML(t.tv_sec);
  170.     s_usec    = INT_CtoML(s.tv_usec);
  171.     s_sec    = INT_CtoML(s.tv_sec);
  172.     g_usec    = INT_CtoML(garbagetime.tv_usec);
  173.     g_sec    = INT_CtoML(garbagetime.tv_sec);
  174. }
  175.